<template>
  <a-modal
    :title="isEdit ? '编辑雷达信息' : '新增雷达信息'"
    :width="750"
    :visible="visible"
    @ok="handleModalConfirm"
    @cancel="handleModalCancel"
    :confirm-loading="modalLoading"
  >
    <a-form-model
      ref="form"
      :model="form"
      :label-col="{ span: 8 }"
      :wrapper-col="{ span: 16 }"
    >
      <a-form-model-item label="新颜白户">
        <a-radio-group v-model="form.notRisk">
          <a-radio :value="0">非白户</a-radio>
          <a-radio :value="1">白户</a-radio>
        </a-radio-group>
      </a-form-model-item>

      <a-form-model-item label="近六个月M0+逾期贷款笔数">
        <a-input
          style="width: 300px"
          v-model="form.sexM0"
          placeholder="请输入"
          allowClear
        />
      </a-form-model-item>

      <a-form-model-item label="贷款行为分数">
        <a-input-number v-model="loanAction_l" :min="0" :max="100" />
        <span style="margin: 0 10px">至</span>
        <a-input-number v-model="loanAction_r" :min="0" :max="100" />
        <span style="margin-left: 10px">如需筛选最大最小一起填写</span>
      </a-form-model-item>
      <a-form-model-item label="还款率(%)">
        <a-input
          style="width: 300px"
          v-model="form.repayRate"
          placeholder="请输入"
          allowClear
        />
      </a-form-model-item>
      <a-form-model-item label="贷款已结清订单数">
        <a-input
          style="width: 300px"
          v-model="form.loanOverOrderCount"
          placeholder="请输入"
          allowClear
        />
      </a-form-model-item>
      <a-form-model-item label="近三个月贷款笔数">
        <a-input-number v-model="threeLoanCount_l" :min="0" :max="100" />
        <span style="margin: 0 10px">至</span>
        <a-input-number v-model="threeLoanCount_r" :min="0" :max="100" />
        <span style="margin-left: 10px">如需筛选最大最小一起填写</span>
      </a-form-model-item>
      <a-form-model-item label="近一个月有履约">
        <a-radio-group v-model="form.oneOfRepay">
          <a-radio :value="1">是</a-radio>
          <a-radio :value="0">否</a-radio>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item label="近一个月履约贷款总金额">
        <a-input
          style="width: 300px"
          v-model="form.oneRepayLoanAmount"
          placeholder="请输入"
          allowClear
        />
        (最小)
      </a-form-model-item>
      <a-form-model-item label="履约笔数">
        <a-input-number v-model="repayOrderCount_l" :min="0" />
        <span style="margin: 0 10px">至</span>
        <a-input-number v-model="repayOrderCount_r" :min="0" />
        <span style="margin-left: 10px">如需筛选最大最小一起填写</span>
      </a-form-model-item>
      <a-form-model-item label="当前逾期笔数">
        <a-input-number v-model="currentOverOrderCount_l" :min="0" />
        <span style="margin: 0 10px">至</span>
        <a-input-number v-model="currentOverOrderCount_r" :min="0" />
        <span style="margin-left: 10px">如需筛选最大最小一起填写</span>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import * as Ajax from "@/api/institution";
export default {
  props: {
    visible: {
      type: Boolean,
    },
    default: {
      type: Object,
      default: () => {},
    },
  },
  computed: {
    isEdit() {
      return Object.keys(this.default).length > 0;
    },
  },
  data() {
    return {
      loanAction_l: "",
      loanAction_r: "",
      threeLoanCount_l: "",
      threeLoanCount_r: "",
      repayOrderCount_l: "",
      repayOrderCount_r: "",
      currentOverOrderCount_l: "",
      currentOverOrderCount_r: "",
      modalLoading: false,
      form: {
        notRisk: "0",
        sexM0: "",
        loanAction: "",
        threeLoanCount: "",
        loanOverOrderCount: "",
        oneOfRepay: "1",
        repayOrderCount: "",
        currentOverOrderCount: "",
        oneRepayLoanAmount: ''
      },
      // formRules: {
      //   sexM0: {
      //     required: true,
      //     message: "请输入近六个月M0+逾期贷款笔数",
      //     trigger: ["blur", "change"],
      //   },
      //   repayRate: {
      //     required: true,
      //     message: "请输入还款率",
      //     trigger: ["blur", "change"],
      //   },
      //   loanOverOrderCount: {
      //     required: true,
      //     message: "请输入贷款已结清订单数",
      //     trigger: ["blur", "change"],
      //   },
      // },
    };
  },
  watch: {
    visible(val) {
      if (val) {
        if (this.isEdit) {
          this.form = JSON.parse(JSON.stringify(this.default));
          if (this.form.loanAction) {
            this.loanAction_l = this.form.loanAction.split("-")[0];
            this.loanAction_r = this.form.loanAction.split("-")[1];
          }
          if (this.form.threeLoanCount) {
            // console.log)
            this.threeLoanCount_l = this.form.threeLoanCount.split("-")[0];
            this.threeLoanCount_r = this.form.threeLoanCount.split("-")[1];
          }
          if (this.form.repayOrderCount) {
            this.repayOrderCount_l = this.form.repayOrderCount.split("-")[0];
            this.repayOrderCount_r = this.form.repayOrderCount.split("-")[1];
          }
          if (this.form.currentOverOrderCount) {
            this.currentOverOrderCount_l =
              this.form.currentOverOrderCount.split("-")[0];
            this.currentOverOrderCount_r =
              this.form.currentOverOrderCount.split("-")[1];
          }

          // console.log(this.form);
        }
      } else {
        // this.handleModalCancel();
      }
    },
  },
  mounted() {},
  methods: {
    handleModalConfirm() {
      // console.log(123);
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.sexM0 =  this.form.sexM0 - 0
          this.form.repayRate =  this.form.repayRate - 0

          this.form.loanOverOrderCount =  this.form.loanOverOrderCount - 0
          this.form.oneRepayLoanAmount =  this.form.oneRepayLoanAmount - 0
          this.form.loanAction = `${this.loanAction_l}-${this.loanAction_r}`;
          this.form.threeLoanCount = `${this.threeLoanCount_l}-${this.threeLoanCount_r}`;
          this.form.repayOrderCount = `${this.repayOrderCount_l}-${this.repayOrderCount_r}`;
          this.form.currentOverOrderCount = `${this.currentOverOrderCount_l}-${this.currentOverOrderCount_r}`;
          const params = { ...this.form };
          // console.log(params);

          const message = this.isEdit ? "编辑成功" : "编辑成功";
          Ajax.riskconfig(params)
            .then(({ success }) => {
              if (success) {
                this.$message.success(message);
                this.$emit("success");
                this.handleModalCancel();
              }
            })
            .finally(() => {
              this.modalLoading = false;
            });
        }
      });
    },
    handleModalCancel() {
      this.loanAction_l = "";
      this.loanAction_r = "";
      this.threeLoanCount_l = "";
      this.threeLoanCount_r = "";
      this.repayOrderCount_l = "";
      this.repayOrderCount_r = "";
      this.currentOverOrderCount_l = "";
      this.currentOverOrderCount_r = "";
      this.form = this.$options.data().form;
      this.$refs.form.resetFields();
      this.$emit("close");
    },
  },
};
</script>

<style lang="scss" scoped>
.table-wrap {
  margin-top: 20px;
  padding-top: 16px;
}

.btn-gruop {
  display: flex;
  justify-content: flex-end;
}

::v-deep .ant-form-item {
  &:last-child {
    margin-bottom: 0;
  }
}
::v-deep .ant-form-item-label {
  text-align: left !important; /* 使用 !important 来确保覆盖其他样式 */
}
</style>
